package org.lc.stk.web.controller.baseInfo;

import java.util.List;
import java.util.stream.Collectors;

import org.lc.stk.model.baseInfo.Cengci;
import org.lc.stk.service.baseInfo.CengciService;
import org.lc.stk.web.dto.cengci.CengciQuery;
import org.lc.stk.web.dto.cengci.CengciResponse;
import org.lc.stk.web.dto.cengci.CreateCengciRequest;
import org.lc.stk.web.dto.cengci.UpdateCengciRequest;
import org.lc.stk.web.response.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;

@RestController
@RequestMapping("/api/v1/baseInfo/cengci")
@Tag(name = "培养层次管理", description = "培养层次的增删改查接口")
@Validated
public class CengciController {

    @Autowired
    private CengciService cengciService;

    @PostMapping
    @Operation(summary = "创建培养层次")
    public ApiResponse<CengciResponse> create(@Valid @RequestBody CreateCengciRequest request) {
        Cengci cengci = cengciService.create(request);
        return ApiResponse.success(CengciResponse.fromEntity(cengci));
    }

    @PutMapping
    @Operation(summary = "更新培养层次")
    public ApiResponse<CengciResponse> update(@Valid @RequestBody UpdateCengciRequest request) {
        Cengci cengci = cengciService.update(request);
        return ApiResponse.success(CengciResponse.fromEntity(cengci));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除培养层次")
    public ApiResponse<Void> delete(
            @Parameter(description = "培养层次ID", required = true)
            @PathVariable Integer id) {
        cengciService.delete(id);
        return ApiResponse.success();
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取培养层次详情")
    public ApiResponse<CengciResponse> getById(
            @Parameter(description = "培养层次ID", required = true)
            @PathVariable Integer id) {
        Cengci cengci = cengciService.getById(id);
        return ApiResponse.success(CengciResponse.fromEntity(cengci));
    }

    @GetMapping("/code/{code}")
    @Operation(summary = "根据编码获取培养层次")
    public ApiResponse<CengciResponse> getByCode(
            @Parameter(description = "培养层次编码", required = true)
            @PathVariable String code) {
        Cengci cengci = cengciService.getByCode(code);
        return ApiResponse.success(CengciResponse.fromEntity(cengci));
    }

    @GetMapping("/list")
    @Operation(summary = "获取所有培养层次")
    public ApiResponse<List<CengciResponse>> list() {
        List<Cengci> cengciList = cengciService.listAll();
        List<CengciResponse> responseList = cengciList.stream()
                .map(CengciResponse::fromEntity)
                .collect(Collectors.toList());
        return ApiResponse.success(responseList);
    }

    @GetMapping
    @Operation(summary = "分页查询培养层次")
    public ApiResponse<List<CengciResponse>> query(
            @Parameter(description = "查询条件")
            CengciQuery query,
            @Parameter(description = "页码(从0开始)", example = "0")
            @RequestParam(defaultValue = "0") int page,
            @Parameter(description = "每页大小", example = "10")
            @RequestParam(defaultValue = "10") int size) {
        
        Sort sort = Sort.by(
            query.getSortDirection().equalsIgnoreCase("desc") ? 
            Sort.Direction.DESC : Sort.Direction.ASC, 
            query.getSortBy()
        );
        
        Page<Cengci> resultPage = cengciService.query(query, PageRequest.of(page, size, sort));
        
        List<CengciResponse> responseList = resultPage.getContent().stream()
                .map(CengciResponse::fromEntity)
                .collect(Collectors.toList());
        
        return ApiResponse.success(
            responseList,
            new ApiResponse.PageInfo(
                resultPage.getNumber(),
                resultPage.getSize(),
                resultPage.getTotalElements(),
                resultPage.getTotalPages()
            )
        );
    }

    @GetMapping("/exists/code/{code}")
    @Operation(summary = "检查编码是否存在")
    public ApiResponse<Boolean> checkCodeExists(
            @Parameter(description = "培养层次编码", required = true)
            @PathVariable String code,
            @Parameter(description = "排除的ID")
            @RequestParam(required = false) Integer excludeId) {
        return ApiResponse.success(cengciService.isCodeExists(code, excludeId));
    }

    @GetMapping("/exists/name/{name}")
    @Operation(summary = "检查名称是否存在")
    public ApiResponse<Boolean> checkNameExists(
            @Parameter(description = "培养层次名称", required = true)
            @PathVariable String name,
            @Parameter(description = "排除的ID")
            @RequestParam(required = false) Integer excludeId) {
        return ApiResponse.success(cengciService.isNameExists(name, excludeId));
    }
}